<?php
namespace Home\Model;
//物品基础属性
class Heart extends Container{
    protected $redis;
    protected $user;
    protected $type ;
    protected $index ;
    protected $username ;
    protected $item_max;
    public function __construct($user){
        //数据集
        parent::__construct($user, "heart");
    }

    public function initData(){
        //初始化有15颗心
        $this->hSet('level', 0);
        $ts = time();
        $this->upgrade($ts);
        $slot = getClass('slot', $this->user);
        $slot->finish('upgrade', 1, $ts);
        $this->add(15);
    }

    //加
    public function add($amount){
        $info = $this->getMax();
        if($info['current'] + $amount > $info['max'])
            $amount = $info['max'] - $info['current'];
        if($amount == 0) return $info['max'];
        return parent::add('heart', $amount);
    }

    public function upgrade($ts, $currency = 'gold'){
        $slot = getClass('slot', $this->user);
        $s = $slot->getFreeSlot('upgrade', 1);
        if(!$s) return false;
        $info = cfg('container', 'counter');
        $level = $this->hGet('level');
        $level ++;
        $money = getClass('money', $this->user);
        if($money->dec($info[$currency][$level-1], $currency, true) === false) return error('金钱不足以升级店铺'.$info[$currency][$level]);
        //减钱
        $money->dec($info[$currency][$level-1], $currency);
        $slot->setSlot('upgrade', 1, $ts, $info['times'][$level-1], 'heart', $level);
        
    }
    public function slotFinish($level){
        $info = cfg('container', 'counter');
        //加等级
        $this->hMset(array('level'=> $level, 'max'=>$info['capcity'][$level-1]));

    }
    
} 